﻿using System;
using System.Collections.Generic;
using System.Text;

namespace HighDiveAdmin.Engine
{
    /// <summary>
    /// List that keeps track of the judges in a contest.
    /// </summary>
    public class JudgeList : List<Judge>
    {
        #region Properties

        /// <summary>
        /// The number of judges in the contest
        /// </summary>
        public int NumberOfJudges { get; set; }

        #endregion

        #region Public Methods

        /// <summary>
        /// Adds a judge to the list.
        /// Throws an exception if the list is full.
        /// </summary>
        /// <param name="judge">A judge who will be added to the list</param>
        public new void Add(Judge judge)
        {
            if (Count < NumberOfJudges)
            {
                base.Add((judge));
            }
            else
            {
                throw new Exception("List is full");
            }
        }

        /// <summary>
        /// Generates strings for judgments login. If the list is not full thrown an exception.
        /// </summary>
        /// <returns>Array of strings for each judge</returns>
        public void GenerateIdStrings()
        {
            if (Count == NumberOfJudges)
            {
                for (int i = 0; i < NumberOfJudges; i++)
                {

                    this[i].IdString = GenerateIdString();
                }
            }
            else
            {
                throw new Exception("judge list is not full");
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Generates a random 5-letter string using symbols A-Z, a-z, 0-9
        /// </summary>
        /// <returns>A random 5-letter string</returns>
        private string GenerateIdString()
        {
            var rnd = new Random();
            StringBuilder builder = new StringBuilder(5);

            for (int j = 0; j < 5; j++)
            {
                int randomNumber = rnd.Next() % 3;
                switch (randomNumber)
                {
                    case 0:
                        randomNumber = rnd.Next() % 10 + 48;
                        break;
                    case 1:
                        randomNumber = rnd.Next() % 26 + 65;
                        break;
                    case 2:
                        randomNumber = rnd.Next() % 26 + 97;
                        break;
                }
                builder.Append(Convert.ToChar(randomNumber));
            }
            return builder.ToString();
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Creates an object of class JudgeList
        /// </summary>
        /// <param name="numberOfJudges">Number of judges list can at most contain. Default = 5</param>
        public JudgeList(int numberOfJudges = 5)
        {
            NumberOfJudges = numberOfJudges;
        }

        #endregion
    }
}
